Imports DTO
Imports System.Data.OleDb

Public Class KhoiDao
    Inherits AbstractDao
    Public Sub New()

    End Sub
    Public Function LaydanhSach() As IList
        Dim ds As New ArrayList()
        Dim cn As OleDbConnection
        Dim strSQL As String
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        strSQL = "Select * From Khoi"
        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()


        Dim khoi As New KhoiDto()


        While (dr.Read())

            khoi = New KhoiDto()
            khoi.MaKhoi = dr("MaKhoi")
            khoi.TenKhoi = dr("TenKhoi")

            ds.Add(khoi)

        End While
        'B5: Dong ket noi CSDL
        cn.Close()
        Return ds


    End Function
    Public Sub Them(ByVal khoiDto As KhoiDto)
        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Insert into Khoi( TenKhoi)) values " & " ( '" & khoiDto.TenKhoi & "')"


        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        cmd.ExecuteNonQuery()

        strSQL = "Select @@IDENTITY"
        cmd = New OleDbCommand(strSQL, cn)
        khoiDto.MaKhoi = cmd.ExecuteScalar()

        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub
    Public Sub CapNhatBang(ByVal dt As DataTable)
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        Dim cn As OleDbConnection
        cn = ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From Khoi"
        'B4: Thuc thi chuoi strSQL
        Dim da As New OleDbDataAdapter(strSQL, cn)
        Dim cb As New OleDbCommandBuilder(da)
        da.Update(dt)
        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub
    Public Function TimKiem(ByVal maKhoi As String)

        Dim khoiDto As New KhoiDto

        Dim cn As OleDbConnection
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = Me.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Select * From Khoi Where MaKhoi = ?"
        'B4: Thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        cmd.Parameters.Add("@MaKhoi", OleDbType.WChar)
        cmd.Parameters("@MaKhoi").Value = maKhoi
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        While (dr.Read())
            khoiDto.MaKhoi = dr("MaKhoi")
            khoiDto.TenKhoi = dr("TenKhoi")

        End While
        'B5: Dong ket noi CSDL
        dr.Close()
        cn.Close()
        Return khoiDto


    End Function




End Class
